package net.vmiranda.jeds.web.reports;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.test.TestRepositoryProducts;

/**
 * Servlet implementation class ReportPrueba
 */
public class ReportPrueba extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReportPrueba() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doReport(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doReport(request, response);
	}

	private void doReport(HttpServletRequest request, HttpServletResponse response){
	try{
	 FastReportBuilder drb = new FastReportBuilder();
        DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
                        .addColumn("Branch", "branch", String.class.getName(),30)
                        .addColumn("Product Line", "productLine", String.class.getName(),50)
                        .addColumn("Item", "item", String.class.getName(),50)
                        .addColumn("Item Code", "id", Long.class.getName(),30,true)
                        .addColumn("Quantity", "quantity", Long.class.getName(),60,true)
                        .addColumn("Amount", "amount", Float.class.getName(),70,true)
                        .addGroups(2)
                        .setTitle("November 2006 sales report")
                        .setSubtitle("This report was generated at " + new Date())
                        .setPrintBackgroundOnOddRows(true)                      
                        .setUseFullPageWidth(true)
                .build();       
        
        JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());   
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);


        
        OutputStream op=response.getOutputStream();
        try{
       
         byte[] bites = JasperExportManager.exportReportToPdf(jp);
         response.setContentType("application/pdf");
         response.setContentLength(bites.length);
         op.write(bites);
         op.close();
         }
         catch(Exception e)    {
          
         }
      
        
		}
        catch(Exception e){
        	
        }

	}
}
